package com.buba.service.yonghu;

import com.buba.dao.AreaMapper;
import com.buba.dao.DeptMapper;
import com.buba.dao.UserMapper;
import com.buba.pojo.Area;
import com.buba.pojo.Dept;
import com.buba.pojo.User;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service("yonghuService")
public class YonghuServiceImpl implements YonghuService{

    Logger log= LogManager.getLogger();
    @Autowired(required = false)
    UserMapper userMapper;
    @Autowired(required = false)
    AreaMapper areaMapper;
    @Autowired(required = false)
    DeptMapper deptMapper;

    @Override
    public List<User> listUser(User user) {
        return userMapper.listUser(user);
    }

    @Override
    public List<Area> listArea() {
        return areaMapper.listOrganization();
    }

    @Override
    public List<Dept> listDept(Integer areaid) {
        return deptMapper.listDept(areaid);
    }

    @Override
    public User getUserByCode(String usercode) {
        return userMapper.findByUsercode(usercode);
    }

    @Override
    public int insertUser(User user) {
        if(user.getAreaid()==0&&user.getDeptid()==0){
            user.setRole(4);
        }else if(user.getAreaid()==1&&user.getDeptid()==0){
            user.setRole(3);
        }else if(user.getAreaid()>1&&user.getDeptid()==0){
            user.setRole(2);
        }else if(user.getAreaid()>1&&user.getDeptid()!=0){
            user.setRole(1);
        }
        user.setIddel("0");
        Date date=new Date();
        user.setCreationdate(date);
        user.setModifydate(date);

        return userMapper.insertSelective(user);
    }

    @Override
    public User getUserInfo(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public int deleteUserById(Integer id) {
        return userMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int updateUser(User user) {
        return userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public int resetPassword(Integer id) {
        User user=new User();
        user.setId(id);
        user.setPassword("123456");
        return userMapper.updateByPrimaryKeySelective(user);
    }
}
